<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="artisan-console" tabindex="-1"><a class="header-anchor" href="#artisan-console"><span>Artisan Console</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a>
<ul>
<li><a href="#tinker">TTinker 命令 （REPL）</a></li>
</ul>
</li>
<li><a href="#writing-commands">编写命令</a>
<ul>
<li><a href="#generating-commands">生成命令</a></li>
<li><a href="#command-structure">命令结构</a></li>
<li><a href="#closure-commands">闭包命令</a></li>
<li><a href="#isolatable-commands">可隔离命令</a></li>
</ul>
</li>
<li><a href="#defining-input-expectations">定义输入期望值</a>
<ul>
<li><a href="#arguments">参数</a></li>
<li><a href="#options">选项</a></li>
<li><a href="#input-arrays">输入数组</a></li>
<li><a href="#input-descriptions">输入说明</a></li>
<li><a href="#prompting-for-missing-input">提示缺少的输入</a></li>
</ul>
</li>
<li><a href="#command-io">I/O 命令</a>
<ul>
<li><a href="#retrieving-input">检索输入</a></li>
<li><a href="#prompting-for-input">输入提示</a></li>
<li><a href="#writing-output">编写输出</a></li>
</ul>
</li>
<li><a href="#registering-commands">注册命令</a></li>
<li><a href="#programmatically-executing-commands">编程式执行命令</a>
<ul>
<li><a href="#calling-commands-from-other-commands">从其他命令调用命令</a></li>
</ul>
</li>
<li><a href="#signal-handling">信号处理</a></li>
<li><a href="#stub-customization">Stub 自定义</a></li>
<li><a href="#events">事件</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>Artisan 是 Laravel 自带的命令行接口。Artisan 以 <code v-pre>artisan</code> 脚本的方式存在于应用的根目录中，并提供了许多有用的命令，来帮你构建应用程序。你可以使用 <code v-pre>list</code> 命令查看所有可用的 Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan list</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>每个命令都有一个“help”帮助界面，它展示并描述了该命令的可用参数和选项。要查看帮助界面，请在命令前加上 <code v-pre>help</code> 即可：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan <span class="token builtin class-name">help</span> migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="laravel-sail" tabindex="-1"><a class="header-anchor" href="#laravel-sail"><span>Laravel Sail</span></a></h4>
<p>如果你使用 <a href="https://learnku.com/docs/laravel/11.x/sail" target="_blank" rel="noopener noreferrer">Laravel Sail</a> 作为本地开发环境，记得使用 <code v-pre>sail</code> 命令行来调用 Artisan 命令。Sail 会在应用的 Docker容器中执行 Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/sail artisan list</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="tinker-repl" tabindex="-1"><a class="header-anchor" href="#tinker-repl"><span>Tinker （REPL）</span></a></h3>
<p>Laravel Tinker 是为 Laravel 提供的一个强大的 REPL（交互式解释器），由 <a href="https://github.com/bobthecow/psysh" target="_blank" rel="noopener noreferrer">PsySH</a> 驱动支持。</p>
<h4 id="介绍-1" tabindex="-1"><a class="header-anchor" href="#介绍-1"><span>介绍</span></a></h4>
<p>所有的 Laravel 应用默认都自带 Tinker。 不过，如果你此前删除了它，你可以使用 Composer 安装：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/tinker</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
在寻找与 Laravel 应用程序交互时能热加载、多行代码编辑和自动补全的功能？试试 <a href="https://tinkerwell.app/" target="_blank" rel="noopener noreferrer">Tinkerwell</a>！</p>
</blockquote>
<h4 id="使用" tabindex="-1"><a class="header-anchor" href="#使用"><span>使用</span></a></h4>
<p>Tinker 允许你在命令行中和整个 Laravel 应用交互，包括 Eloquent 模型、队列、事件等等。要进入 Tinker 环境，只需运行 <code v-pre>tinker</code> Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan tinker</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>vendor:publish</code> 命令发布 Tinker 的配置文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--provider</span><span class="token operator">=</span><span class="token string">"Laravel\Tinker\TinkerServiceProvider"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
<code v-pre>dispatch</code> 辅助函数及 <code v-pre>Dispatchable</code> 类中 <code v-pre>dispatch</code> 方法依赖于垃圾回收机制将任务放置到队列中。因此，使用 tinker 时，请使用 <code v-pre>Bus::dispath</code> 或 <code v-pre>Queue::push</code> 来分发任务。</p>
</blockquote>
<h4 id="命令白名单" tabindex="-1"><a class="header-anchor" href="#命令白名单"><span>命令白名单</span></a></h4>
<p>Tinker 使用一个“白名单”来确定哪些 Artisan 命令可以在其 shell 中运行。默认情况下，你可以允许<br>
<code v-pre>clear-compiled</code>，<code v-pre>down</code>， <code v-pre>env</code>，<code v-pre>inspire</code>，<code v-pre>migrate</code>，<code v-pre>optimize</code>， 和 <code v-pre>up</code> 命令。如果你想允许更多命令，你可以将它们添加到 <code v-pre>tinker.php</code>配置文件的 <code v-pre>commands</code> 数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'commands'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// App\Console\Commands\ExampleCommand::class,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="别名黑名单" tabindex="-1"><a class="header-anchor" href="#别名黑名单"><span>别名黑名单</span></a></h4>
<p>一般来说， Tinker会在你引入类时自动为其添加别名。不过，你可能希望永远不为某些类设置别名。你可以在<code v-pre>thinker.php</code>配置文件的 <code v-pre>dont_alias</code> 数组中列举这些类来完成此操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'dont_alias'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="编写命令" tabindex="-1"><a class="header-anchor" href="#编写命令"><span>编写命令</span></a></h2>
<p>除了 Artisan 提供的命令之外，你还可以创建自定义命令。一般来说，命令会保存在 <code v-pre>app/Console/Commands</code> 目录中；不过，你可以自由选择命令的存储位置，只要它能够被 Composer 加载即可。</p>
<h3 id="生成命令" tabindex="-1"><a class="header-anchor" href="#生成命令"><span>生成命令</span></a></h3>
<p>要创建新命令，你可以使用 <code v-pre>make:command</code> Artisan 命令。该命令会在 <code v-pre>app/Console/Commands</code> 目录下创建一个新的命令类。不用担心你的应用程序中不存在这个目录 - 它会在第一次运行 <code v-pre>make:command</code> Artisan 命令的时自动创建：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:command SendEmails</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="命令结构" tabindex="-1"><a class="header-anchor" href="#命令结构"><span>命令结构</span></a></h3>
<p>生成命令后，你应该为类的 <code v-pre>signature</code> 和 <code v-pre>description</code> 属性定义适当的值。这些属性会在<code v-pre>list</code>屏幕展示你的命令时被用到。<code v-pre>signature</code> 属性还允许你定义<a href="#defining-input-expectations">命令输入预期值</a>。 <code v-pre>handle</code> 方法会在命令执行时被调用。你可以在该方法中编写命令逻辑。</p>
<p>让我们看一个示例命令。请注意，我们能够通过命令的 handle 方法请求我们需要的任何依赖项。Laravel <a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 将自动注入此方法签名中带有类型提示的所有依赖项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Commands</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>DripEmailer</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Command</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">SendEmails</span> <span class="token keyword">extends</span> <span class="token class-name">Command</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 控制台命令的名称和签名</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'mail:send {user}'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 命令描述</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$description</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Send a marketing email to a user'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 执行命令</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">DripEmailer</span> <span class="token variable">$drip</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$drip</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
为了更好地复用代码，让你的控制台命令保持轻量级并委托应用服务来完成它们的任务。在上面的例子中，请注意我们注入了一个服务类来进行发送电子邮件的「繁重工作」。</p>
</blockquote>
<h4 id="退出代码" tabindex="-1"><a class="header-anchor" href="#退出代码"><span>退出代码</span></a></h4>
<p>如果<code v-pre>handle</code>方法没有返回任何内容并且命令执行成功，则命令将以退出代码<code v-pre>0</code>来退出，表示成功。当然，<code v-pre>handle</code>方法也可以选择返回一个整数来手动指定命令的退出代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Something went wrong.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想在命令里的任意方法里让命令以「失败」结束，你可以使用<code v-pre>fail</code>方法。<code v-pre>fail</code>方法将立即终止命令的执行并返回<code v-pre>1</code>的退出代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Something went wrong.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="闭包命令" tabindex="-1"><a class="header-anchor" href="#闭包命令"><span>闭包命令</span></a></h3>
<p>闭包命令提供了定义控制台命令为类的另一种选择。就像路由闭包是对控制器的一种替代，可以认为命令闭包是对命令类的一种替代。</p>
<p>尽管 <code v-pre>routes/console.php</code> 文件不定义 HTTP 路由，它定义了进入你的应用程序的基于控制台的入口点（路由）。在这个文件中，你可以使用 <code v-pre>Artisan::command</code> 方法定义所有的基于闭包的控制台命令。<code v-pre>command</code> 方法接受两个参数：<a href="#defining-input-expectations">命令的签名</a>  和一个闭包，闭包接收命令的参数和选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send {user}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"Sending email to: <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$user</span><span class="token punctuation">}</span></span>!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该闭包绑定到底层的命令实例，因而你可以完全访问所有你通常能够在完整命令类上访问的辅助方法</p>
<h4 id="类型约束依赖" tabindex="-1"><a class="header-anchor" href="#类型约束依赖"><span>类型约束依赖</span></a></h4>
<p>除了接受命令参数及选项外，命令闭包也可以使用类型约束从 <a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 中解析其他的依赖关系：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>DripEmailer</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send {user}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">DripEmailer</span> <span class="token variable">$drip</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$drip</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="闭包命令的描述" tabindex="-1"><a class="header-anchor" href="#闭包命令的描述"><span>闭包命令的描述</span></a></h4>
<p>定义基于闭包的命令时，可以使用 <code v-pre>purpose</code> 方法为命令添加描述。这个描述将在运行 <code v-pre>php artisan list</code> 或 <code v-pre>php artisan help</code> 命令时显示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send {user}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">purpose</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'向一个用户发送营销邮件'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="可隔离的命令" tabindex="-1"><a class="header-anchor" href="#可隔离的命令"><span>可隔离的命令</span></a></h3>
<blockquote>
<p>[警告]<br>
要使用此功能，您的应用必须将 <code v-pre>memcached</code>、<code v-pre>redis</code>、<code v-pre>dynamodb</code>、<code v-pre>database</code>、<code v-pre>file</code> 或 <code v-pre>array</code> 缓存驱动设置为默认缓存驱动。此外，所有服务器都必须与同一个中央缓存服务器进行通信。</p>
</blockquote>
<p>有时候，您可能希望确保一次只能运行一个命令实例。为此，您可以在命令类上实现 <code v-pre>Illuminate\Contracts\Console\Isolatable</code> 接口：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Commands</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Command</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Isolatable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">SendEmails</span> <span class="token keyword">extends</span> <span class="token class-name">Command</span> <span class="token keyword">implements</span> <span class="token class-name">Isolatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当一个命令被标记为 <code v-pre>Isolatable</code> 时，Laravel 会自动给该命令添加一个 <code v-pre>--isolated</code> 选项。使用该选项调用命令时，Laravel 会确保没有其他实例的该命令正在运行。Laravel 通过尝试使用应用的默认缓存驱动获取一个原子锁来实现这一点。如果有其他实例的命令正在运行，那么该命令将不会执行；不过，命令仍然会以一个成功的退出状态码结束：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan mail:send <span class="token number">1</span> <span class="token parameter variable">--isolated</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您希望指定命令在无法执行时应返回的退出状态码，可以通过 <code v-pre>isolated</code> 选项提供所需的状态码：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan mail:send <span class="token number">1</span> <span class="token parameter variable">--isolated</span><span class="token operator">=</span><span class="token number">12</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="锁-id" tabindex="-1"><a class="header-anchor" href="#锁-id"><span>锁 ID</span></a></h4>
<p>默认情况下，Laravel 会使用命令的名称生成字符串的键，用于获取应用程序缓存中的原子锁。当然，你可以在 Artisan 命令类中定义一个 <code v-pre>isolatableId</code> 方法来自定义这个键，从而允许你将命令的参数或选项集成到键中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取命令的可隔离ID</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">isolatableId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="锁过期时间" tabindex="-1"><a class="header-anchor" href="#锁过期时间"><span>锁过期时间</span></a></h4>
<p>默认情况下，隔离锁会在命令完成后过期。或者，如果命令被中断而无法完成，锁将在一小时后过期。但是，您可以通过在命令上定义一个<code v-pre>isolationLockExpiresAt</code>方法来调整锁的过期时间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">DateTimeInterface</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">DateInterval</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指定命令的隔离锁何时过期</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">isolationLockExpiresAt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name">DateTimeInterface</span><span class="token operator">|</span><span class="token class-name">DateInterval</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="定义输入期望值" tabindex="-1"><a class="header-anchor" href="#定义输入期望值"><span>定义输入期望值</span></a></h2>
<p>编写控制台命令时，通常是通过参数和选项来收集用户输入的。 Laravel 让你可以非常方便地在 <code v-pre>signature</code> 属性中定义你期望用户输入的内容。<code v-pre>signature</code> 属性允许使用单一且可读性高，类似路由的语法来定义命令的名称、参数和选项。</p>
<h3 id="参数" tabindex="-1"><a class="header-anchor" href="#参数"><span>参数</span></a></h3>
<p>用户提供的所有参数和选项都用花括号括起来。在下面的示例中，该命令定义了一个必需的参数 <code v-pre>user</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 命令的名称和标识。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'mail:send {user}'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以使参数可选或为参数定义默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 可选参数...</span></span>
<span class="line"><span class="token string single-quoted-string">'mail:send {user?}'</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 带有默认值的可选参数...</span></span>
<span class="line"><span class="token string single-quoted-string">'mail:send {user=foo}'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="选项" tabindex="-1"><a class="header-anchor" href="#选项"><span>选项</span></a></h3>
<p>选项，如同参数，是另一种形式的用户输入。选项在通过命令行提供时，前缀为两个连字符（<code v-pre>--</code>）。选项有两种类型：接收值的选项和不接收值的选项。不接收值的选项作为一个布尔“开关”。让我们来看一个这种类型选项的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 控制台命令的名称和签名。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'mail:send {user} {--queue}'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中，调用 Artisan 命令时可以指定 <code v-pre>--queue</code> 开关。如果传递了 <code v-pre>--queue</code> 开关，选项的值为 <code v-pre>true</code>。否则，值为 <code v-pre>false</code>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan mail:send <span class="token number">1</span> <span class="token parameter variable">--queue</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="带值的选项" tabindex="-1"><a class="header-anchor" href="#带值的选项"><span>带值的选项</span></a></h4>
<p>接下来，让我们看一个期望值的选项。如果用户必须为选项指定一个值，你应该在选项名称后加上一个 <code v-pre>=</code> 符号：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 控制台命令的名称和签名。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'mail:send {user} {--queue=}'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中，用户可以这样为选项传递一个值。如果在调用命令时没有指定选项，其值将为 <code v-pre>null</code>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan mail:send <span class="token number">1</span> <span class="token parameter variable">--queue</span><span class="token operator">=</span>default</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过在选项名称后指定默认值来为选项分配默认值。如果用户没有传递选项值，将使用默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mail:send {user} {--queue=default}'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="选项快捷方式" tabindex="-1"><a class="header-anchor" href="#选项快捷方式"><span>选项快捷方式</span></a></h4>
<p>在定义选项时，你可以在选项名称前指定一个快捷方式，并使用 <code v-pre>|</code> 字符作为分隔符来分隔快捷方式和完整的选项名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mail:send {user} {--Q|queue}'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在终端上调用命令时，选项快捷方式应以单个连字符为前缀，并且在为选项指定值时不应包含 <code v-pre>=</code> 字符：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan mail:send <span class="token number">1</span> <span class="token parameter variable">-Qdefault</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="输入数组" tabindex="-1"><a class="header-anchor" href="#输入数组"><span>输入数组</span></a></h3>
<p>如果你想定义期望接收多个输入值的参数或选项，你可以使用 <code v-pre>*</code> 字符。首先，让我们看一个指定这种参数的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mail:send {user*}'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>调用这个方法时，<code v-pre>user</code> 参数可以按顺序传递给命令行。例如，以下命令将设置 <code v-pre>user</code> 的值为一个包含 <code v-pre>1</code> 和 <code v-pre>2</code> 的数组：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan mail:send <span class="token number">1</span> <span class="token number">2</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个 <code v-pre>*</code> 字符可以与可选参数定义结合使用，以允许零个或多个参数实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mail:send {user?*}'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="选项数组" tabindex="-1"><a class="header-anchor" href="#选项数组"><span>选项数组</span></a></h4>
<p>在定义期望接收多个输入值的选项时，传递给命令的每个选项值都应以选项名称为前缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mail:send {--id=*}'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>通过传递多个 <code v-pre>--id</code> 参数可以调用这样的命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan mail:send <span class="token parameter variable">--id</span><span class="token operator">=</span><span class="token number">1</span> <span class="token parameter variable">--id</span><span class="token operator">=</span><span class="token number">2</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="输入描述" tabindex="-1"><a class="header-anchor" href="#输入描述"><span>输入描述</span></a></h3>
<p>你可以通过使用冒号分隔参数名称和描述来为输入参数和选项分配描述。如果你需要更多空间来定义你的命令，可以随意将定义分布在多行上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 控制台命令的名称和签名。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'mail:send</span>
<span class="line">                        {user : 用户的ID}</span>
<span class="line">                        {--queue : 作业是否应该被加入队列}'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="提示参数缺失" tabindex="-1"><a class="header-anchor" href="#提示参数缺失"><span>提示参数缺失</span></a></h3>
<p>如果你的命令包含必需的参数，当这些参数未提供时用户会收到一个错误消息。作为替代，你可以配置你的命令在必需的参数缺失时自动提示用户，通过实现 <code v-pre>PromptsForMissingInput</code> 接口来实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Commands</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Command</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>PromptsForMissingInput</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">SendEmails</span> <span class="token keyword">extends</span> <span class="token class-name">Command</span> <span class="token keyword">implements</span> <span class="token class-name">PromptsForMissingInput</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * The name and signature of the console command.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'mail:send {user}'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果 Laravel 需要从用户那里收集一个必需的参数，它将自动询问用户该参数，通过智能地使用参数名称或描述来提问。如果你希望自定义用于收集必需参数的问题，你可以实现 <code v-pre>promptForMissingArgumentsUsing</code> 方法，返回一个以参数名称为键的问题数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Prompt for missing input arguments using the returned questions.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">promptForMissingArgumentsUsing</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Which user ID should receive the mail?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以通过使用包含问题和占位符的元组来提供占位文本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Which user ID should receive the mail?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'E.g. 123'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想要完全控制提示，你可以提供一个闭包，该闭包应该提示用户并返回他们的答案：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>search</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">search</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Search for a user:'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">placeholder</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'E.g. Taylor Otwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">options</span><span class="token punctuation">:</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span></span>
<span class="line">            <span class="token operator">?</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"%<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$value</span><span class="token punctuation">}</span></span>%"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!NOTE]<br>
关于可用提示及其使用的更多信息，请参阅全面的 <a href="https://learnku.com/docs/laravel/11.x/prompts" target="_blank" rel="noopener noreferrer">Laravel Prompts</a> 文档。</p>
</blockquote>
<p>如果您希望提示用户选择或输入<a href="#options">选项</a>，您可以在命令的 <code v-pre>handle</code> 方法中包含提示。但是，如果您只希望在用户因缺失参数而自动被提示时再向他们提示，那么您可以实现 <code v-pre>afterPromptingForMissingArguments</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Input<span class="token punctuation">\</span>InputInterface</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Output<span class="token punctuation">\</span>OutputInterface</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Prompts<span class="token punctuation">\</span>confirm</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 用户被提示缺失参数后执行的操作。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">afterPromptingForMissingArguments</span><span class="token punctuation">(</span><span class="token class-name type-declaration">InputInterface</span> <span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">OutputInterface</span> <span class="token variable">$output</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$input</span><span class="token operator">-></span><span class="token function">setOption</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token function">confirm</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">label</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Would you like to queue the mail?'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">default</span><span class="token punctuation">:</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'queue'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="命令输入-输出" tabindex="-1"><a class="header-anchor" href="#命令输入-输出"><span>命令输入/输出</span></a></h2>
<h3 id="获取输入" tabindex="-1"><a class="header-anchor" href="#获取输入"><span>获取输入</span></a></h3>
<p>在命令执行过程中，您可能需要访问命令接受的参数和选项的值。要做到这一点，您可以使用 <code v-pre>argument</code> 和 <code v-pre>option</code> 方法。如果参数或选项不存在，将返回 <code v-pre>null</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 执行控制台命令。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$userId</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您需要以数组形式检索所有参数，调用 <code v-pre>arguments</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$arguments</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">arguments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>选项可以像参数一样轻松地使用 <code v-pre>option</code> 方法检索。要以数组形式检索所有选项，调用 <code v-pre>options</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 检索一个特定的选项...</span></span>
<span class="line"><span class="token variable">$queueName</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'queue'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 以数组形式检索所有选项...</span></span>
<span class="line"><span class="token variable">$options</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">options</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="提示输入" tabindex="-1"><a class="header-anchor" href="#提示输入"><span>提示输入</span></a></h3>
<blockquote>
<p>[!NOTE]<br>
<a href="https://learnku.com/docs/laravel/11.x/prompts" target="_blank" rel="noopener noreferrer">Laravel Prompts</a> 是一个 PHP 包，为您的命令行应用程序添加了美观且用户友好的表单，具备浏览器类特性，包括占位符文本和验证功能。</p>
</blockquote>
<p>除了显示输出以外，你还可以要求用户在执行命令期间提供输入。<code v-pre>ask</code> 方法将询问用户指定的问题来接收用户输入，然后用户输入将会传到你的命令中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 执行命令</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">ask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>ask</code> 方法还接受一个可选的第二个参数，该参数指定了如果没有用户提供输入时的默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">ask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>secret</code> 方法与 <code v-pre>ask</code> 相似，区别在于用户的输入将不可见。这个方法在需要输入一些诸如密码之类的敏感信息时是非常有用的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">secret</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is the password?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="请求确认" tabindex="-1"><a class="header-anchor" href="#请求确认"><span>请求确认</span></a></h4>
<p>如果你需要请求用户进行一个简单的确认，可以使用 <code v-pre>confirm</code> 方法来实现。默认情况下，这个方法会返回 <code v-pre>false</code>。当然，如果用户输入 <code v-pre>y</code> 或 <code v-pre>yes</code>，这个方法将会返回 <code v-pre>true</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">confirm</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Do you wish to continue?'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>必要的话，你可以将 <code v-pre>true</code> 作为第二个参数传递给 <code v-pre>confirm</code> 方法，这样就可以在默认情况下返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">confirm</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Do you wish to continue?'</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自动补全" tabindex="-1"><a class="header-anchor" href="#自动补全"><span>自动补全</span></a></h4>
<p><code v-pre>anticipate</code> 方法可用于为可能的选项提供自动补全功能。用户依然可以忽略自动补全的提示，进行任意回答：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">anticipate</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>另外，您可以把一个闭包作为第二个参数传递给<code v-pre>anticipate</code>方法。每当用户输入一个字符时，该闭包就会被调用。此闭包应接收一个包含用户截至目前输入内容的字符串形式的参数，并返回一个自动补全选项的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">anticipate</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'What is your address?'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// Return auto-completion options...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="多选问题" tabindex="-1"><a class="header-anchor" href="#多选问题"><span>多选问题</span></a></h4>
<p>如果你需要在询问问题时给用户提供一组预先设定好的选项，你可以使用 <code v-pre>choice</code> 方法。你可以给方法的第三个参数传入数组的索引，来设置没有选项被选择时的默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">choice</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$defaultIndex</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外， <code v-pre>choice</code> 方法接受第四和第五个可选参数 ，用于确定选择有效响应的最大尝试次数以及是否允许多次选择：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">choice</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'What is your name?'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$defaultIndex</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$maxAttempts</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$allowMultipleSelections</span> <span class="token operator">=</span> <span class="token constant boolean">false</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写输出" tabindex="-1"><a class="header-anchor" href="#编写输出"><span>编写输出</span></a></h3>
<p>你可以使用 <code v-pre>line</code>，<code v-pre>info</code>，<code v-pre>comment</code>，<code v-pre>question</code> ，<code v-pre>warn</code>和 <code v-pre>error</code> 方法，发送输出到控制台。 这些方法中的每一个都会使用合适的 ANSI 颜色以展示不同的用途。例如，我们要为用户展示一些常规信息。通常，<code v-pre>info</code> 将会以绿色文本在控制台展示。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 执行命令</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The command was successful!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>展示错误信息，使用 <code v-pre>error</code> 方法。错误信息通常使用红色字体显示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Something went wrong!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>line</code> 方法输出普通的无色文本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Display this on the screen'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>newLine</code> 方法输出空白行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 输出单行空白...</span></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 输出三行空白...</span></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="表格" tabindex="-1"><a class="header-anchor" href="#表格"><span>表格</span></a></h4>
<p><code v-pre>table</code> 方法可以轻松正确地格式化多行 / 多列数据。你需要做的就是提供表的列名和数据，Laravel 会自动为你计算合适的表格宽度和高度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">table</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Email'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="进度条" tabindex="-1"><a class="header-anchor" href="#进度条"><span>进度条</span></a></h4>
<p>对于长时间运行的任务，显示一个进度条来通知用户任务的完成程度会很有帮助。使用 <code v-pre>withProgressBar</code> 方法，Laravel 将显示一个进度条，并在给定的可迭代值上推进每次迭代的进度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">withProgressBar</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">performTask</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有时，你可能需要更多手动控制进度条的前进方式。首先，定义流程将迭代的步骤总数。然后，在处理完每个项目后推进进度条：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$bar</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">output</span><span class="token operator">-></span><span class="token function">createProgressBar</span><span class="token punctuation">(</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token variable">$users</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$bar</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">performTask</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$bar</span><span class="token operator">-></span><span class="token function">advance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$bar</span><span class="token operator">-></span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
有关更多高级选项，请查看 <a href="https://symfony.com/doc/7.0/components/console/helpers/progressbar.html" target="_blank" rel="noopener noreferrer">Symfony Progress Bar component documentation</a>.</p>
</blockquote>
<h2 id="注册命令" tabindex="-1"><a class="header-anchor" href="#注册命令"><span>注册命令</span></a></h2>
<p>默认情况下，Laravel会自动注册<code v-pre>app/Console/Commands</code>目录中的所有命令。但是，你可以让Laravel使用应用程序的<code v-pre>bootstrap/app.php</code>文件中的<code v-pre>withCommands</code> 方法扫描其他目录以查找Artisan命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">-></span><span class="token function">withCommands</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../app/Domain/Orders/Commands'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>必要的话，您还可以通过将命令的类名提供给<code v-pre>withCommands</code>方法来手动注册命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Domain<span class="token punctuation">\</span>Orders<span class="token punctuation">\</span>Commands<span class="token punctuation">\</span>SendEmails</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token operator">-></span><span class="token function">withCommands</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name static-context">SendEmails</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当Artisan启动时，应用程序中的所有命令都将被<a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a>并注册到 Artisan。</p>
<h2 id="编程式执行命令" tabindex="-1"><a class="header-anchor" href="#编程式执行命令"><span>编程式执行命令</span></a></h2>
<p>有时你可能希望在 CLI 之外执行 Artisan 命令。例如，你可能希望从路由或控制器执行 Artisan 命令。你可以使用 <code v-pre>Artisan</code> 门面的 <code v-pre>call</code> 方法来完成此操作。 <code v-pre>call</code> 方法接受命令的签名名称或类名作为其第一个参数，并接受一个命令参数数组作为第二个参数。退出代码会最后被返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Artisan</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/user/{user}/mail'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$exitCode</span> <span class="token operator">=</span> <span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'--queue'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'default'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以将整个 Artisan 命令作为字符串传递给 <code v-pre>call</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send 1 --queue=default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="传递数组值" tabindex="-1"><a class="header-anchor" href="#传递数组值"><span>传递数组值</span></a></h4>
<p>如果你的命令定义了一个接受数组的选项，你可以将一个数组值传递给该选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Artisan</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/mail'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$exitCode</span> <span class="token operator">=</span> <span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'--id'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="传递布尔值" tabindex="-1"><a class="header-anchor" href="#传递布尔值"><span>传递布尔值</span></a></h4>
<p>如果你需要指定不接受字符串值的选项的值，例如 <code v-pre>migrate:refresh</code> 命令上的 <code v-pre>--force</code> 标志，则应传递 <code v-pre>true</code> 或 <code v-pre>false</code> 作为 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$exitCode</span> <span class="token operator">=</span> <span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'migrate:refresh'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'--force'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="队列-artisan-命令" tabindex="-1"><a class="header-anchor" href="#队列-artisan-命令"><span>队列 Artisan 命令</span></a></h4>
<p>使用 <code v-pre>Artisan</code> 门面的 <code v-pre>queue</code> 方法，你甚至可以对 Artisan 命令进行排队，以便你的 <a href="https://learnku.com/docs/laravel/10.x/queues" target="_blank" rel="noopener noreferrer">队列工作者</a> 在后台处理它们。在使用此方法之前，请确保你已配置队列并正在运行队列侦听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Artisan</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/user/{user}/mail'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'--queue'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'default'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用<code v-pre>onConnection</code>和<code v-pre>onQueue</code>方法来指定Artisan命令应分发到的连接或队列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Artisan</span><span class="token operator">::</span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'--queue'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'default'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">onConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'redis'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">onQueue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'commands'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="从其他命令调用命令" tabindex="-1"><a class="header-anchor" href="#从其他命令调用命令"><span>从其他命令调用命令</span></a></h3>
<p>有时你可能希望从现有的 Artisan 命令调用其他命令。你可以使用 <code v-pre>call</code> 方法来执行此操作。<code v-pre>call</code> 方法接受命令名称和命令参数 / 选项的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Execute the console command.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'--queue'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'default'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想调用另一个控制台命令并禁用它的输出，你可以使用 <code v-pre>callSilently</code> 方法。 <code v-pre>callSilently</code> 方法与 <code v-pre>call</code> 方法具有相同的签名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">callSilently</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail:send'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'--queue'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'default'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="信号处理" tabindex="-1"><a class="header-anchor" href="#信号处理"><span>信号处理</span></a></h2>
<p>如你所知，操作系统可以向运行中的进程发送信号。例如，「SIGTERM」信号是操作系统要求程序终止的方式。如果你想在 Artisan 控制台命令中监听信号，并在信号出现时时执行代码，你可以使用 <code v-pre>trap</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 执行命令</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">trap</span><span class="token punctuation">(</span><span class="token constant">SIGTERM</span><span class="token punctuation">,</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">shouldKeepRunning</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">shouldKeepRunning</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要一次侦听多个信号，你可以向 <code v-pre>trap</code> 方法提供一个信号数组。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">trap</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant">SIGTERM</span><span class="token punctuation">,</span> <span class="token constant">SIGQUIT</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$signal</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">shouldKeepRunning</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$signal</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// SIGTERM / SIGQUIT</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="stub-定制" tabindex="-1"><a class="header-anchor" href="#stub-定制"><span>Stub 定制</span></a></h2>
<p>Artisan 控制台的 <code v-pre>make</code> 命令用于创建各种类，例如控制器、队列、迁移和测试。这些类是使用「stub」文件生成的，这些文件中会根据你的输入填充值。但是，你可能需要对 Artisan 生成的文件进行少量更改。为此，你可以使用 <code v-pre>stub:publish</code> 命令将最常见的 stub 命令发布到你的应用程序中，以便可以自定义它们：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan stub:publish</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>已发布的 stub 将存放于你的应用根目录下的 <code v-pre>stubs</code> 目录中。对这些 stub 进行任何改动都将在你使用 Artisan 的 <code v-pre>make</code> 命令生成相应的类的时候反映出来。</p>
<h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>Artisan 在运行命令时会调度三个事件： <code v-pre>Illuminate\Console\Events\ArtisanStarting</code>， <code v-pre>Illuminate\Console\Events\CommandStarting</code>，和 <code v-pre>Illuminate\Console\Events\CommandFinished</code>。当 Artisan 开始运行时，会立即调度 <code v-pre>ArtisanStarting</code> 事件。接下来，在命令运行之前立即调度 <code v-pre>CommandStarting</code> 事件。最后，一旦命令执行完毕，就会调度 <code v-pre>CommandFinished</code> 事件。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/artisanmd/16671" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/ar...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/artisanmd/16671" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/ar...</a></p>
</blockquote>
</div></template>


